
import { upload } from '../../../js/modules/material.js'
import { util } from '../../../js/util';
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  properties: {
    data: {
      value: null,
      type: Object
    },
    componentStyle: {
      type: String,
      value: 'v1'
    },
  },
  data: {
    uploadImageList: [],
    photolist:[],
  },
  behaviors: [componentBehavior],
  methods: {
    onDelImageEvent(e) {
      var that = this;
      var imgidx = e.currentTarget.dataset.imgidx;
      var image_list = that.data.uploadImageList;
      var photolist = that.data.photolist;
      photolist.splice(imgidx,1)
      image_list.splice(imgidx, 1);
      that.setData({
        uploadImageList: image_list,
        photolist:photolist
      })
      // console.log(photolist,'====1');
      this.onChange()
    },
    bindUploadEvent() {
      var that = this;
      let maxCount = this.data.data.MaxCount
      if (this.data.uploadImageList.length >= maxCount) {
        util.tips("最多只能上传"+ maxCount +"张");
        return;
      }
      var count = maxCount - this.data.uploadImageList.length;
      wx.chooseImage({
        count: count,
        sizeType: ['compressed'],
        sourceType: ['album'],
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;
          tempFilePaths.map(function (item) {
            that.data.uploadImageList.push(item);
          })
          that.setData({
            uploadImageList: that.data.uploadImageList
          }); 
          var data = {num:1010,type:1,tempFilePaths:tempFilePaths};
          upload.upload_image_server(data, function (image_list) {
            if (image_list.length==0){
              util.alert("上传失败");
              return;
            }
            image_list.map(function (item) {
              that.data.photolist.push(item);
              that.onChange()
            });
          })
        },
        fail (err) {
          console.log('选择图片失败', err)
        }
      });
    },
    onChange() {
      let list = this.data.photolist
      let dataId = this.data.data.Id
      this.triggerEvent('uploadImageCallBack', {
        dataId: dataId,
        value: list
      })
    }
  }
})
